package com.ywy.chat.server;

import lombok.extern.slf4j.Slf4j;
import org.tio.server.ServerGroupContext;
import org.tio.websocket.server.WsServerStarter;
import org.tio.websocket.server.handler.IWsMsgHandler;

import java.io.IOException;

/**
 * 群聊服务启动器
 *
 * @author ywy
 * @date 2021/12/31 14:06
 */
@Slf4j
public class ChatServerStarter {
    /**
     * WS服务启动类
     */
    private WsServerStarter starter;

    public ChatServerStarter(int port) throws IOException {
        // 初始化IWsMsgHandler
        IWsMsgHandler handler = new WsMsgHandler();
        starter = new WsServerStarter(port, handler);

        // 配置ServerGroupContext
        ServerGroupContext serverGroupContext = starter.getServerGroupContext();
        // 设置心跳超时时间
        serverGroupContext.setHeartbeatTimeout(50000);
    }

    /**
     * 启动服务
     * @throws IOException
     */
    public void start() throws IOException {
        this.starter.start();
        log.info("ImServerStarter----->tio server start!");
    }
}
